﻿#!/usr/bin/env python

import time
import threading
from Queue import *

#消息队列
msgcache = Queue(1000)

#用来派发消息
class ProcessMsg(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        #post msg from msgcache
        while True:
            #print 'Process Msg'
            if msgcache.empty():
                pass
            else:
                msg = msgcache.get()
                #给在线列表广播数据
                BroadcastMsg(msg)

def BroadcastMsg(msg):
    
    for key in cache.dic.keys():
        player = cache.dic.get(key)
        if not player:
            continue
        else:
            if player.LastFetch < msg.time:
                player.PushMsg(msg)
            else:
                pass
        
class Msg():
    
    def __init__(self):
        self.msg  = ''
        self.timestamp = ''
        self.name = ''
        self.time = 0
        
    def SetMsg(self, msg):
        self.msg = msg

    def SetTime(self):
        self.time = time.time()
        self.timestamp = time.strftime('%H:%M:%S', time.localtime())
        
    def SetName(self, name):
        self.name = name
        
    def GetJason(self):
        return ("[{time:%s,name:'%s',msg:'%s'}"%(self.timestamp,self.name,self.msg))

#pack msg
def PackMsg(info):
    print 'packmsg'
    msg = Msg()
    msg.SetTime()
    msg.SetName('Me')
    msg.SetMsg(info)
    
    
